#include "headers/timer.h"
#include "../interrupts/headers/idt.h"
#include "../tools/headers/common.h"
#include "../api/headers/io.h"
#include "../interrupts/headers/irq.h"
#include "headers/task.h"

u32int timer_ticks = 0;
volatile BOOL critical_section = false;

static void timer_handler(registers_type registers)
{
   timer_ticks++;
   if(!critical_section)
	switch_task();
   //printf("%d\n", timer_ticks);
}

void init_timer(u32int frequency)
{
   register_interrupt_handler(IRQ0, timer_handler);
   u32int divisor = 1193180 / frequency;
   outb(0x43, 0x36);
   outb(0x40, divisor & 0xFF);
   outb(0x40, divisor >> 8);
} 

void raise_timer_int()
{
	asm volatile("int $32");
}